//
//  ProItemViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/27.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "ProItemViewController.h"
#import "ItemModel.h"               /// 传进来的model的类
#import "SelectControlView.h"       /// 选择控制视图

@interface ProItemViewController ()
@property (nonatomic, retain)UIWebView *webView;
@end

@implementation ProItemViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.model = [[ItemModel alloc] init];
        self.urlString = [[NSString alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 二级页面隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 重写返回上级页面方法
    UIImage *backImage = [[UIImage imageNamed:@"返回.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popBack:)];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 14)];
    [self.view addSubview:self.webView];
    /// 判断加载的url
    if (self.model.url != nil) {
        NSURL *url = [NSURL URLWithString:self.model.url];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:urlRequest];
    } else if (self.urlString != nil){
        NSURL *url = [NSURL URLWithString:self.urlString];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:urlRequest];
    }
    
    //navbar上的控制器
    SelectControlView *selectView = [[SelectControlView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * .5f, 0, 140, 40)];
    selectView.tag = 188;
    [self.navigationController.navigationBar addSubview:selectView];
    
    [selectView.come addTarget:self action:@selector(comeAndBackAction:) forControlEvents:UIControlEventTouchUpInside];
    [selectView.back addTarget:self action:@selector(comeAndBackAction:) forControlEvents:UIControlEventTouchUpInside];
    [selectView.collection addTarget:self action:@selector(collectionAction:) forControlEvents:UIControlEventTouchUpInside];
    [selectView.share addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
}
// 前进/后退
- (void)comeAndBackAction:(UIButton *)button
{
    switch (button.tag) {
        case 220:
        {
            [self.webView goBack];
        }
            break;
        case 221:
        {
            [self.webView goForward];
        }
            break;
            
        default:
            break;
    }
}

// 收藏
- (void)collectionAction:(UIButton *)button
{
    
}
// 分享
- (void)shareAction:(UIButton *)button
{
    
}

// 返回上级页面
- (void)popBack:(UIBarButtonItem *)sender
{
    UIView *view = [self.navigationController.navigationBar viewWithTag:188];
    [view removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
